﻿package src.controler 
{
  import flash.events.MouseEvent;
  import org.papervision3d.core.proto.MaterialObject3D;
  import org.papervision3d.materials.MovieMaterial;
  import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Cube;
  import src.event.proOperateEvent;
  import src.view.flatFaceView;
	
	/**
	 * 产品建筑类
	 * 代理内部元件的行为
	 * @author	MeatHill
	 * @version 0.1(2009-06-04)
	 */
	public class product_builder extends DisplayObject3D
	{
    private static const QUALITY:uint = 10;
    
		private var _item_arr:Array;
		private var _data_xlist:XMLList;
		private var _single_item:DisplayObject3D;
		
		public function product_builder(data:XMLList) 
		{
			_data_xlist = data;
			var _item_num:uint = _data_xlist.length();
			if (_item_num == 1) {
				_single_item = produce_component(_data_xlist[0]);
        addChild(_single_item);
			} else {
				_item_arr = new Array();
			}
		}
		
		/****************
		 * properties
		 * *************/
		
		/****************
		 * functions
		 * *************/
    /**
     * 生产模块
     * @param	xml
     * @return
     */
		private function produce_component(xml:XML):DisplayObject3D {
			var _type:String = xml.@type;
			switch(_type) {
				case 'cube':
					return create_cube(xml);
					break;
					
				default:
					break;
			}
		}
    /**
     * 生成面
     * @param	xml
     * @return
     */
    private function create_face(xml:XML):MaterialObject3D {
      //trace(xml.area.toXMLString());
      var _face_mc:flatFaceView = new flatFaceView(uint(xml.@width), uint(xml.@height), xml.@src);
      _face_mc.add_areas(xml.area);
      _face_mc.addEventListener(proOperateEvent.PRODUCT_OPERATE, component_operate_handler);
      _face_mc.addEventListener(proOperateEvent.SHOW_ALT, show_alt_msg);
      _face_mc.addEventListener(proOperateEvent.REMOVE_ALT, remove_alt_msg);
      var _face:MovieMaterial = new MovieMaterial(_face_mc, false, true, true);
      //_face.smooth = true;
      _face.interactive = true;
      
      //图片加载完毕之后的回调事件
      _face_mc.add_material_callback(_face);
      
      return _face;
    }
    /**
     * 生成立方体
     * @param	xml
     * @return 立方体
     */
		private function create_cube(xml:XML):Cube {
      var _faces_obj:Object = new Object();
			for each (var _face in xml.face) {
        _faces_obj[_face.@name] = create_face(_face);
      }
      var _material_list:MaterialsList = new MaterialsList(_faces_obj);
      var _cube:Cube = new Cube(_material_list, uint(xml.@width), uint(xml.@height), uint(xml.@length), QUALITY);
      _cube.name = xml.@name;
			_cube.rotationY = 180;
      return _cube;
		}
    private function component_operate_handler(evt:proOperateEvent):void {
      switch(evt.op_type) {
        case 'show_msg':
        
          break;
          
        default:
          break;
      }
    }
    private function show_alt_msg(evt:proOperateEvent):void {
      dispatchEvent(evt);
    }
    private function remove_alt_msg(evt:proOperateEvent):void {
      dispatchEvent(evt);
    }
		
		/****************
		 * methods
		 * *************/
		public function play_movie(tar:XML, src:String):void {
      
    }
	}
}